// pages/mechAllteach/mechAllteach.js
var util=require("../../utils/util");
var home=require("../../api/home");


Page({
  /*页面的初始数据*/
  data: {
    id: '',
    currentPage: 1,
    pageSize: 10,
    hasMoreDate: true,
    latitude: 22.80536,
    longitude: 113.29321,
    teach_list: [],
    educations:['小学','初中','高中','专科','本科','硕士','博士']
  },
  /*生命周期函数--监听页面加载*/
  onLoad: function (options) {
    var that=this;
    wx.getStorage({
      key: 'city',
      success:function(res){
        var info=res.data;
        that.setData({latitude: info.lat,longitude: info.lng,id:options.id});
        //
        that.get_teach_list(options.id);
      }
    })
  },
  nav_teach_detail:function(e){
    var uid=e.currentTarget.dataset.uid,oid=e.currentTarget.dataset.oid;
    wx.navigateTo({
      url: '../famousDetail/famousDetail?uid='+uid+'&oid='+oid+'&type=3',
    })
  },
  // 触底
  onReachBottom:function(){
    var that=this,hasMoreData=that.data.hasMoreData,id=that.data.id;
    if(hasMoreData){
      that.get_teach_list(id);
    }else{
      util.page_show_toast('没有更多了',1500);
    }
  },
  // 获取名师列表
  get_teach_list:function(id){
    wx.showLoading({title: '加载中'})
    var that=this,educations=that.data.educations,currentPage=that.data.currentPage,pageSize=that.data.pageSize;
    var params={
      "businessId": id,
      "currentPage": currentPage,
      "pageSize": pageSize,
      "lat": that.data.latitude,
      "lng": that.data.longitude
    }
    home.get_teach_list(params).then(res=>{
      if(res.data.code==200){
        var list=res.data.data.data;
        for(var i=0;i<list.length;i++){
          list[i].realName=list[i].realName?(list[i].realName.indexOf("老师")>-1?list[i].realName:list[i].realName+"老师"):'';
          list[i].yearCount=list[i].teachingTime?new Date().getFullYear()-new Date(list[i].teachingTime).getFullYear()+1:'';
          list[i].count=list[i].studentCount>10000?Math.ceil(list[i].studentCount/10000)+'万':list[i].studentCount;
          list[i].educaName=list[i].education?educations[Number(list[i].education)-1]:'';
          list[i].positiveRate=list[i].positiveRate?list[i].positiveRate*100+'%':'';
        }
        var teach_list = that.data.teach_list;
        teach_list = util.extend(teach_list, list)
        if (list.length < pageSize) {
          that.setData({
            teach_list: teach_list,
            hasMoreData: false
          })
        } else {
          that.setData({
            teach_list: teach_list,
            hasMoreData: true,
            currentPage: currentPage + 1
          })
        }
      }
    })
  },
})